// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'package:flutter/material.dart';

import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:junyeyishu/utils/utils.dart';

class QGWebWalletView extends StatefulWidget {
  String? url;
  QGWebWalletView({super.key, this.url});
  @override
  State<QGWebWalletView> createState() => _QGWebWalletViewState();
}

class _QGWebWalletViewState extends State<QGWebWalletView> {
  String? title;
  var controller = WebViewController();

  @override
  void initState() {
    super.initState();

    controller.setNavigationDelegate(NavigationDelegate(
      onPageFinished: (url) async {
        title = await controller.getTitle();
        if (mounted) {
          setState(() {});
        }
      },
    ));
    controller.setJavaScriptMode(JavaScriptMode.unrestricted);
    controller.addJavaScriptChannel("pay_success",
        onMessageReceived: (ret) async {
      print("pay_success");
      print(ret.message);
    });

    controller.loadRequest(Uri.parse(widget.url ?? ""));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xFFF8F8F6),
        title: Text(
          title ?? "",
          style: TextStyle(
              color: Color(0xFF111111),
              fontSize: 16.rpx,
              fontWeight: FontWeight.normal),
        ),
        leading: TextButton(
            onPressed: () async {
              bool canGoBack = await controller.canGoBack();
              if (canGoBack) {
                controller.goBack();
              } else {
                Navigator.of(context).pop();
              }
            },
            child: QGImage(asset: "nav_back")),
      ),
      body: WebViewWidget(controller: controller),
    );
  }
}
